﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;

public partial class administrator_usercontrol_prodmanager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Lbthongbao.Text = "";
            List<LoaiSanPhamBUS> loaisps = new List<LoaiSanPhamBUS>();
            loaisps = LoaiSanPhamBUS.GetAll();
            DropDownList1.DataSource = loaisps;
            DropDownList1.DataTextField = "TenLoai";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            if (Request.QueryString["cm"] == null)
            {
                LbID.Text = "-1";
                List<SanPhamBUS> sanphams = SanPhamBUS.GetAll();
                GridView1.AutoGenerateColumns = false;
                GridView1.DataSource = sanphams;
                GridView1.DataBind();
                
            }
            else
            {
                string cm = Request.QueryString["cm"];
                mt1.ActiveViewIndex = 1;
                if (cm == "edit"&&Request.QueryString["ID"]!=null)
                {
                    int ID = Convert.ToInt32(Request.QueryString["ID"]);
                    SanPhamBUS sp = SanPhamBUS.GetSanphamByID(ID);
                    TextBox1.Text = sp.MaSanPham;
                    TextBox2.Text = sp.TenSanPham;
                    TextBox3.Text = sp.strSoTon;
                    TextBox4.Text = sp.Gia.ToString() ;
                    TextBox5.Text = sp.BaoHanh;
                    TextBox6.Text = sp.MoTa;
                    DropDownList1.SelectedValue = sp.LoaiSanPham.ID.ToString();
                    LbID.Text = sp.ID.ToString();
                    Label2.Text = sp.Anh;
                }
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        List<SanPhamBUS> sanphams = SanPhamBUS.GetAll();
        GridView1.DataSource = sanphams;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int ID = Convert.ToInt32(e.CommandArgument);
            SanPhamBUS sp = SanPhamBUS.GetSanphamByID(ID);

            try
            {
                sp.Delete();
                Response.Redirect("sanpham.aspx");
            }
            catch
            {
                Lbthongbao.Text = "Không Thể Xóa Dữ Liệu !!!";
            }

        }
        if (e.CommandName == "Edit")
        {
            Response.Redirect("sanpham.aspx?cm=edit&ID=" + e.CommandArgument.ToString());
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lkbXoa");

            l.Attributes.Add("onclick", "javascript:return " +
                 "confirm('bạn có muốn xóa sản phẩm " +
                 DataBinder.Eval(e.Row.DataItem, "MaSanPham") + " ?')");

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int ID = -1;
        try { ID = Convert.ToInt32(LbID.Text); }
        catch { }
        string Hinh = Label2.Text;
        string MaSanPham = TextBox1.Text;
        string TenSanPham = TextBox2.Text;
        int IDLoai = Convert.ToInt32(DropDownList1.SelectedValue);
        int soluong = 0;
        try { soluong = Convert.ToInt32(TextBox3.Text); }
        catch { }
        decimal giaban = 0;
        try { giaban = (decimal)Convert.ToDecimal(TextBox4.Text);}
        catch { }
        if (FileUpload1.HasFile && FileUpload1.FileName != "")
        {
            Hinh = "Upload\\Shop\\" + FileUpload1.FileName;
            string link = Server.MapPath("~/Upload/shop/");
            FileUpload1.SaveAs(link+FileUpload1.FileName);
        }
        string baohanh = TextBox5.Text;
        string mota = TextBox6.Text;
        SanPhamBUS sp = new SanPhamBUS()
        {
            ID=ID,
            MaSanPham=MaSanPham,
            TenSanPham=TenSanPham,
            SoTon=soluong,
            MoTa=mota,
            LoaiSanPham=new LoaiSanPhamBUS()
            {
                ID=IDLoai,
            },
            Anh=Hinh,
            Gia=giaban,
            BaoHanh=baohanh,
        };
        if (sp.ID == -1)
        {
            try
            {
                sp.Insert();
                Lbthongbao.Text = "";
                Response.Redirect("sanpham.aspx");
            }
            catch
            {
                Lbthongbao.Text = "không thể thêm sản phâm";
            }
        }
        else
        {
            try
            {
                sp.Update();
                Lbthongbao.Text = "sửa thành công";
            }
            catch
            {
                Lbthongbao.Text = "sửa thất bại";
            }
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("sanpham.aspx");
    }
}
